package com.cn.yunhe.struts.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.cn.yunhe.struts.modle.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class FileupdateController extends ActionSupport implements ModelDriven<Object>{
	private static final long serialVersionUID = 1L;

	private User user;//struts中当声明的属性有类的时候必须实现ModelDriven这个拦截器
	public void uplodfile() throws IOException{
		
		String root=ServletActionContext.getServletContext().getRealPath("/upload");
		System.out.println("root:"+root);
		InputStream is=new FileInputStream(user.getUserhead());
		System.out.println("userhead:"+user.getUserhead());
		OutputStream os=new FileOutputStream(new File(root, user.getUserheadFileName()));
		System.out.println("userheadFileName:"+user.getUserheadFileName());
		byte[] buffer=new byte[1024];
		int len=0;
		while((len =is.read(buffer))!=-1){
			System.out.println("len"+len);
			os.write(buffer, 0, len);
		}
		os.close();
		is.close();
	}
	
	// 要下载文件在服务器上的路径
	private String fileid;
	// 要下载文件的文件名
	private String fileName;

	public String getFileid() {
		return fileid;
	}

	public void setFileid(String fileid) {
		this.fileid = fileid;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public User getModel() {
		// TODO Auto-generated method stub
		if(user==null){
			user=new User();
			user.setUsername("王小乐");
			
		}
		return user;
	}
	
	public InputStream getDownFile(){
		Map<String, String> fileMap=new HashMap<String, String>();
		fileMap.put("100100100225747478", "zhangsan.jpg");
		fileMap.put("100100100225747479", "lisi.png");
		this.fileName = fileMap.get(fileid);
		
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + "222222.png");
	}

	public String execute() throws Exception {
		return SUCCESS;
	}


}
